/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI.Tree;

import Common.IDocument;
import Common.IProject;
import Project.Document;
import Project.DocumentName;
import UI.AddDialog;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;

/**
 *
 * @author Администратор
 */
public class ProjectNode extends DefaultMutableTreeNode implements ITree, IContextCommand {

    private IProject project;
    private RootNode rootNode;
    private JPopupMenu popupMenu;

    public ProjectNode(IProject project, RootNode rootNode) {
        super(project.getName(), true);
        this.rootNode = rootNode;
        this.project = project;
        //--------------------------------------------------------------------
        
        //----------------------------------------
        this.popupMenu = new JPopupMenu();
        JMenu jmi = new JMenu("Create Document");
        jmi.add(new ContextCommandItem(this, "Create Shem"));
        jmi.add(new ContextCommandItem(this, "Create Station"));
        this.popupMenu.add(jmi);
        this.popupMenu.add(new ContextCommandItem(this, "Rename Project"));
        this.popupMenu.add(new ContextCommandItem(this, "Delete Project"));
        //-----------------------------------------------
        
        this.setUserObject(this.project.getName());

        for (IDocument doc : project.getDocuments()) {
            this.add(new DefaultMutableTreeNode(doc.getName()));
        }
    }

    // <editor-fold defaultstate="collapsed" desc="ITree Initialize">
    public JTree getTree() {
        return this.rootNode.getTree();
    }

    public JPopupMenu getPopupMenu() {
        return this.popupMenu;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="IContextCommand Initialize">
    public void DoCommand(ContextCommandItem item) {
        if (item.getText().contains("Create Shem") || item.getText().contains("Create Station")) {
            AddDialog adp = new AddDialog(null, true, "Добавить", "Введите название документа");
            adp.setVisible(true);
            if (AddDialog.DISPOSE_ON_CLOSE != adp.getDefaultCloseOperation()) {
                Document doc = new Document(this.project);
                if (item.getText().contains("Create Shem")) {
                    doc.setDoucmentType(DocumentName.Shems.toString());
                }
                if (item.getText().contains("Create Station")) {
                    doc.setDoucmentType(DocumentName.Station.toString());
                }
                doc.setName(adp.getValue());
                this.project.getDocuments().add(doc);
                this.add(new DocumentNode(doc, this));
                //this.getTree().expandRow();
            }
        }
        if (item.getText().contains("Delete Project")) {
            this.rootNode.remove(this);
        }
        if (item.getText().contains("Rename Project")) {
            AddDialog add = new AddDialog(null, true, "Применить", "Переименовать проэкт");
            add.setValue(this.project.getName());
            add.setVisible(true);
            if (AddDialog.DISPOSE_ON_CLOSE != add.getDefaultCloseOperation()) {
                this.project.setName(add.getValue());
                this.setUserObject(this.project.getName());
            }
        }
        this.getTree().updateUI();
    }
    // </editor-fold>

    @Override
    public void remove(MutableTreeNode aChild) {
        this.project.getDocuments().remove(((DocumentNode) aChild).getDocument());
        super.remove((DefaultMutableTreeNode) aChild);
        this.getTree().updateUI();
    }

    public IProject getProject() {
        return this.project;
    }
}
